package com.example.myapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.bean.LiveDataBean;
import com.example.myapplication.util.ScreenUtils;

import java.util.List;

public class LiveAdapter extends RecyclerView.Adapter<LiveAdapter.LiveHolder> {


    private List<LiveDataBean> data;
    private Context mContext;

    public LiveAdapter(Context context) {
        mContext = context;
    }

    /**
     * 减去分割线的间距
     */
    @NonNull
    @Override
    public LiveHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_video, parent, false);
        ViewGroup.LayoutParams layoutParams = inflate.getLayoutParams();
        int width = ScreenUtils.getScreenWidth(mContext) / 3;
        layoutParams.width = width;
        layoutParams.height = (int) (width * 1.26f);

        inflate.setLayoutParams(layoutParams);
        return new LiveHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull LiveHolder holder, int position) {
        LiveDataBean liveDataBean = data.get(position);
        Glide.with(mContext).load(liveDataBean.getUrlCover()).into(holder.ivBg);
        holder.tvCount.setText(String.valueOf(liveDataBean.getViewers()));

    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public void setData(List<LiveDataBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    static class LiveHolder extends RecyclerView.ViewHolder {

        private final ImageView ivBg;
        private final TextView tvCount;

        public LiveHolder(@NonNull View itemView) {
            super(itemView);
            ivBg = (ImageView) itemView.findViewById(R.id.iv_bg);
            tvCount = (TextView)itemView.findViewById(R.id.tv_count);
        }
    }
}

